<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Shop extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public $tyList = array(

    );

    public function _initialize()
    {
        $this->_model = new \app\admin\model\Shop();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);
        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
        $this->tyList=getNewWindProductClass();
        $this->tyList = changeKeyForList($this->tyList,'id');
        $this->assign('tyList', $this->tyList);
    }

    public function shangData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 0), $id);
        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    public function xiaData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 1), $id);

        if ($res || $res === 0) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }
    }

    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_ty = trim(input("request.ser_ty"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }
        if ($ser_ty > 0) {
            $where['ty'] = $ser_ty;
        }
        if ($ser_stats == '0' || $ser_stats == '1') {
            $where['stats'] = $ser_stats;
        }
        $where['is_delete'] = '0';
        $lists = $this->_model->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');
        $lists['data'] = (new \app\admin\mapper\Common())->listMapper($lists['data']);
        $this->assign('lists', $lists);

        $this->assign('request', input("request."));
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    public function editGoodAttr($id, $attr)
    {
        $model = new \app\admin\model\GoodAttribute();
        if($attr!="[]" && $attr!="") {
            $oldinfo = $model->getDataForWhere(array(
                'good_id' => $id
            ));
            if (empty($oldinfo)) {
                $this->addGoodAttr($id, $attr);
            } else {
                $data = array(
                    'attr' => $attr,
                );
                $w = array(
                    'good_id' => $id
                );
                $model->updateDataForWhere($data, $w);
            }
        }
        else
        {
            $model->delData(array(
                'good_id' => $id
            ));
        }


    }

    public function addGoodAttr($id, $attr)
    {
        if($attr!="[]" && $attr!="") {
            $model = new \app\admin\model\GoodAttribute();
            $data = array(
                'good_id' => $id,
                'attr' => $attr,
            );
            $model->add($data);
        }
    }

    public function addData()
    {
        $sys_opt = trim(input("request.sys_opt"));//auth
        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $content = trim(input("request.content"));//auth
            $attr = trim(input("request.attr"));//auth
            $data2 = array(
                'content' => $content,
                'uid' => $this->LoginUserInfo['UserInfo']['id'],
                'ctime' => time(),
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->add($data);
            if ($res) {
                $this->addGoodAttr($res, $attr);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $title = "添加";
        $this->assign('title', $title);
        return view('addData');
    }

    public function runRewardDate()
    {
        $days = trim(input("request.days"));//auth
        $task_time = trim(input("request.task_time"));//auth
        $open_reward_time = $this->runRewardTime($days, $task_time);
        $return = ['err' => 0, 'msg' => '操作成功', 'data' => $open_reward_time];
        return json($return);
    }

    public function infoData()
    {

        $id = trim(input("request.id"));//auth

        $info = $this->_model->getDataForWhere(array('id' => $id));

        $info['UserInfo'] = array();
        if ($info['uid']) {
            $info['UserInfo'] = getUserInfoForID($info['uid']);
        }

        $this->assign('info', $info);
        $title = "详情";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');

    }


    public function editData()
    {

        $id = trim(input("request.id"));//auth
        $sys_opt = trim(input("request.sys_opt"));//auth
        //查询用户
        $info = $this->_model->getDataForWhere(array('id' => $id));

        if ($sys_opt) {
            $data1 = $_REQUEST['data'];
            $content = trim(input("request.conts"));//auth
            $attr = trim(input("request.attr"));//auth
            $w = array(
                'id' => $id
            );
            $data2 = array(
                'content' => $content,
            );
            $data = array_merge($data1, $data2);
            $res = $this->_model->updateDataForWhere($data, $w);

            if ($res || ($res === 0)) {
                $this->editGoodAttr($id, $attr);
                $return = ['err' => 0, 'msg' => '操作成功'];
                return json($return);
            } else {
                $return = ['err' => 3, 'msg' => '操作失败'];
                return json($return);
            }
        }
        $tmpGoodAttribute = getGoodAttr($info['id']);
        $goodAttribute =array();
        if($tmpGoodAttribute)
        {
            $goodAttribute = json_decode($tmpGoodAttribute['attr'],true);
        }
        $this->assign('goodAttribute', $goodAttribute);

        $this->assign('info', $info);
        //设置全局信息
        $title = "编辑";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('editData');
    }

    public function saleLists()
    {
        $class_id = trim(input("request.class_id"));//auth
        $ser_username = trim(input("request.ser_username"));//auth
        $ser_min_num = trim(input("request.ser_min_num"));//auth
        $ser_max_num = trim(input("request.ser_max_num"));//auth
        $ser_is_win = trim(input("request.ser_is_win"));//auth
        $ser_is_yuer = trim(input("request.ser_is_yuer"));//auth
        if ($ser_min_num && $ser_max_num) {
            $where['num'] = array(
                array('egt', $ser_min_num), array('elt', $ser_max_num)
            );
        }
        if (!empty($ser_username)) {
            $tempUserLists = getUserListsForNickname($ser_username, 'id');
            $userIds = array();
            foreach ($tempUserLists as $key => $val) {
                $userIds[] = $val['id'];
            }
            $where['uid'] = array(
                'in', $userIds
            );
        }
        if ($ser_is_win == '0' || $ser_is_win == '1' || $ser_is_win == "2") {
            $where['is_win'] = $ser_is_win;
        }

        if ($ser_is_yuer == '0' || $ser_is_yuer == '1') {
            $where['is_yuer'] = $ser_is_yuer;
        }

        $where['classid'] = $class_id;
        $classUserjoinModel = new \app\admin\model\ClassUserjoin();
        $lists = $classUserjoinModel->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid');

        $this->assign('lists', $lists);
        $this->assign('class_id', $class_id);

        $this->assign('request', input("request."));
        return view('saleLists');
    }
}